package com.wframe.project.jnd.tag.web;

import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.wframe.framework.spring.SpringContextHolder;
import com.wframe.project.jnd.services.HotwordService;

public class HotwordTag extends RequestContextAwareTag{
	
	private static final long serialVersionUID = 1L;
	HotwordService hotwordService;
	private List<Map> list;
	private int indexId=0;

	@Override
	protected int doStartTagInternal() throws Exception {
		// TODO Auto-generated method stub
		indexId=0;
		hotwordService=SpringContextHolder.getBean(HotwordService.class);
		list=hotwordService.queryHotword(6);
		if(next()){
			return EVAL_BODY_INCLUDE;
		}else return 0;
	}

	public int doAfterBody(){
		if(next()){
			return EVAL_BODY_AGAIN;
		}else return 0;
	}
	
	public int doEndTag(){
		list=null;
		return 0;
	}
	
	private boolean next(){
		if(list!=null && indexId<list.size()){
			Map bean=list.get(indexId);
			Object[] keys=bean.keySet().toArray();
			for(Object key:keys)
				pageContext.getRequest().setAttribute(key.toString(),bean.get(key.toString()));
			indexId++;
			return true;
		}else return false;
	}
}
